<?	// Adapted from the default WordPress theme
if(!function_exists('imagecreatetruecolor')) {
	die();
}
$corners	=	array(0 => 0,1 => 0,2 => 0,3 => 0,4 => 0,5 => 1,6 => 1,7 => 1,8 => 2,9 => 2,10 => 3,11 => 4,12 => 5,13 => 6,14 => 7,15 => 8,16 => 10,17 => 14,);
	
if($_POST['dryan_rounded_bg']) {
	$bg = preg_replace('/[^0-9A-Fa-f]/','',$_POST['dryan_rounded_bg']);
} else {
	$bg = 'ffffff';
}

if($_POST['dryan_rounded_header_bg']) {
	$fg_header = preg_replace('/[^0-9A-Fa-f]/','',$_POST['dryan_rounded_header_bg']);
} else {
	$fg_header = '000000';
}

if($_POST['dryan_rounded_sidebar_bg']) {
	$fg_sidebar = preg_replace('/[^0-9A-Fa-f]/','',$_POST['dryan_rounded_sidebar_bg']);
} else {
	$fg_sidebar = '000000';
}

if($_POST['dryan_rounded_footer_bg']) {
	$fg_footer = preg_replace('/[^0-9A-Fa-f]/','',$_POST['dryan_rounded_footer_bg']);
} else {
	$fg_footer = '000000';
}

the_image($corners,$bg,$fg_header,'header_right');
the_image($corners,$bg,$fg_header,'header_left');
the_image($corners,$bg,$fg_sidebar,'sidebar_right');
the_image($corners,$bg,$fg_sidebar,'sidebar_left');
the_image($corners,$bg,$fg_sidebar,'sidebar_btm_right');
the_image($corners,$bg,$fg_sidebar,'sidebar_btm_left');
the_image($corners,$bg,$fg_footer,'footer_right');
the_image($corners,$bg,$fg_footer,'footer_left');
the_image($corners,$fg_header,$bg,'menu_left');
the_image($corners,$fg_header,$bg,'menu_right');

function color_hex2dec ($color) { // From http://us3.php.net/manual/en/function.imagecreatetruecolor.php
	return array (hexdec (substr ($color, 0, 2)), hexdec (substr ($color, 2, 2)), hexdec (substr ($color, 4, 2)));
}

function the_image($corners,$bg,$fg,$direction = 'btm_left') {
	
	list ($r1, $g1, $b1) = color_hex2dec ($fg); // Foreground
	list ($r2, $g2, $b2) = color_hex2dec ($bg); // Background

	$w = 18;
	$h = 18;
	$im = @imagecreatetruecolor($w, $h);
	$bg = imagecolorallocate($im,$r2,$g2,$b2);
	imagefill($im,0,0,$bg);

	for ( $i = 0; $i < $h; $i++ ) {
		$color = imagecolorallocate( $im, $r1, $g1, $b1 );
		if ( array_key_exists($i, $corners) ) {
			$x = $corners[$i];
		}
		imageline( $im, $x, $i, $w, $i, $color );
		if($i == 0) {
			imagesetpixel($im,$x,$i,$color);
		} elseif(in_array($i,range(1,4))) {
			$this_r = round((($r2 - $r1) / (6 - $i)) + $r1);
			$this_g = round((($g2 - $g1) / (6 - $i)) + $g1);
			$this_b = round((($b2 - $b1) / (6 - $i)) + $b1);
			$antialias = imagecolorallocate($im,$this_r,$this_g,$this_b);
			imagesetpixel($im,$x,$i,$antialias);
		} elseif(in_array($i,range(6,7))) {
			$this_r = round((($r2 - $r1) / (9 - $i)) + $r1);
			$this_g = round((($g2 - $g1) / (9 - $i)) + $g1);
			$this_b = round((($b2 - $b1) / (9 - $i)) + $b1);
			$antialias = imagecolorallocate($im,$this_r,$this_g,$this_b);
			imagesetpixel($im,$x,$i,$antialias);
		} elseif(in_array($i,range(8,9))) {
			$this_r = round((($r2 - $r1) / (11 - $i)) + $r1);
			$this_g = round((($g2 - $g1) / (11 - $i)) + $g1);
			$this_b = round((($b2 - $b1) / (11 - $i)) + $b1);
			$antialias = imagecolorallocate($im,$this_r,$this_g,$this_b);
			imagesetpixel($im,$x,$i,$antialias);
		} elseif(in_array($i,range(10,12))) {
			$this_r = round((($r2 - $r1) / ($i - 8)) + $r1);
			$this_g = round((($g2 - $g1) / ($i - 8)) + $g1);
			$this_b = round((($b2 - $b1) / ($i - 8)) + $b1);
			$antialias = imagecolorallocate($im,$this_r,$this_g,$this_b);
			imagesetpixel($im,$x,$i,$antialias);
		} elseif(in_array($i,range(13,14))) {
			$this_r = round((($r2 - $r1) / (16 - $i)) + $r1);
			$this_g = round((($g2 - $g1) / (16 - $i)) + $g1);
			$this_b = round((($b2 - $b1) / (16 - $i)) + $b1);
			$antialias = imagecolorallocate($im,$this_r,$this_g,$this_b);
			imagesetpixel($im,$x,$i,$antialias);
		} elseif(in_array($i,range(15,16))) {
			$this_r = round((($r2 - $r1) / 2) + $r1);
			$this_g = round((($g2 - $g1) / 2) + $g1);
			$this_b = round((($b2 - $b1) / 2) + $b1);
			$antialias = imagecolorallocate($im,$this_r,$this_g,$this_b);
			imagesetpixel($im,$x,$i,$antialias);
			$this_r = round((($r2 - $r1) / 4) + $r1);
			$this_g = round((($g2 - $g1) / 4) + $g1);
			$this_b = round((($b2 - $b1) / 4) + $b1);
			$antialias = imagecolorallocate($im,$this_r,$this_g,$this_b);
			imagesetpixel($im,$x + 1,$i,$antialias);
		} elseif($i == 17) {
			$n = 2;
			for($m = 1; $m <= 5; $m++) {
				$this_r = round((($r2 - $r1) / $m) + $r1);
				$this_g = round((($g2 - $g1) / $m) + $g1);
				$this_b = round((($b2 - $b1) / $m) + $b1);
				$antialias = imagecolorallocate($im,$this_r,$this_g,$this_b);
				imagesetpixel($im,$x - $n,$i,$antialias);
				$n--;
			}
		}
	}
	if($direction == 'header_right' || $direction == 'sidebar_btm_right') {
		$im = imagerotate($im,90,$bg);
	} elseif($direction == 'sidebar_right' || $direction == 'footer_right' || $direction == 'menu_right') {
		$im = imagerotate($im,180,$bg);
	} elseif($direction == 'sidebar_left' || $direction == 'footer_left' || $direction == 'menu_left') {
		$im = imagerotate($im,270,$bg);
	}
	imagepng($im,dirname(__FILE__) . "/" . $direction . ".png");
	imagedestroy($im);
}
?>